import { IWidget, LGraphCanvas, LGraphNode, Vector2, widgetTypes } from "litegraph.js";
// 参考于 litegraph.js文件的   LGraphCanvas.prototype.drawNodeWidgets
export class ExameButton implements IWidget {
    public clicked: boolean = false;

    public name: string = "水电费";

    public value: any;

    public disabled: any = false; // 是否可以点击

    constructor(value: any) {
        this.value = value;
    }

    public computeSize(width: number): [number, number] {
        return [width, 30];
    }

    public draw(ctx: CanvasRenderingContext2D, node: LGraphNode, width: number, posY: number, height: number): void {
        const margin = 15;
        if (this.clicked) {
            ctx.fillStyle = "#AAA";
            this.clicked = false;
        }
        ctx.fillRect(margin, posY, width - margin * 2, height);
        if (!this.disabled) ctx.strokeRect(margin, posY, width - margin * 2, height);
        ctx.textAlign = "center";
        ctx.fillStyle = "#FF0000";
        ctx.fillText("内容", width * 0.5, posY + height * 0.7);
    }

    public mouse(event: MouseEvent, pos: Vector2, node: LGraphNode): boolean {
        if (event.type === "mousemove" || event.type === "mousedown") {
            return false;
        }
        // if (this.callback) {
        //     setTimeout(() => {
        //         this.callback(this.value, this, node, pos, event);
        //     }, 20);
        // }
        this.clicked = true;
        return true;
    }

    public callback(value: this["value"], graphCanvas: LGraphCanvas, node: LGraphNode, pos: Vector2, event: MouseEvent | undefined): void {
        // console.log("callback", value, graphCanvas, node, pos, event);
    }
}
